<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">

    <title>Tongji Search</title>

    <!-- Bootstrap core CSS -->
    <link href="styles/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="styles/cover.css" rel="stylesheet">
    <link href="styles/index.css" rel="stylesheet">
    <link href="styles/result.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="scripts/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body id="body">
  <div>
    <h3 class="masthead-brand">Tongji Search</h3>
    <nav>
      <ul class="nav pull-right">
        <li id="date">2015年6月1日</li>
      </ul>
      <script>
        function showTime(){
          var date = new Date();
          document.getElementById("date").innerHTML = date.toLocaleString();
        }
        showTime();
      </script>
    </nav>
  </div>

    <div class="site-wrapper" id="index">
      <div class="site-wrapper-inner">
        <div class="cover-container" id="main">
          <div class="masthead clearfix">

          </div>
          <div class="inner cover">
            <div class="tongji-logo">
              <img src="images/logo.png">
            </div>
            <div class="search-box">
              <div class="search-container search-form">
                <div class="dropdown">
                  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                    全部
                    <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                    <li><a id="all" href="#" onclick="selected(id)">全部</a> </li>
                    <li><a id="sse" href="#" onclick="selected(id)">sse</a></li>
                    <li><a id="youth" href="#" onclick="selected(id)">校团委</a></li>
                    <li><a id="news" href="#" onclick="selected(id)">新闻网</a></li>
                  </ul>
                </div>
              </div>



              <div class="search-container search-form">
                <input type="text" id="search-input" name="key" placeholder="搜索同济" class="form-control search-input-text">
                <input type="submit" id="search-btn" value="" class="search-input-btn">
              </div>
              <!--<form id="search-form" class="form-signin search-form">-->
              <!--</form>-->
            </div>
            <div class="message-div" id="new-message" style="display: none;">
              <div>

              </div>
              <p>最新消息</p>
            </div>
          </div>
          <div class="mastfoot">
            <div class="inner">
              <p><a href="http://localhost:8080">同济搜索</a>, 专注于更好的校内搜索</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script>
      document.getElementById("main").style.width = window.screen.width;
    </script>


    <div id="result" hidden="true">
      <div id="topBar">
        <div class="container">
          <img src="images/logo.png"/>
        </div>
        <div class="container">
          <div class="dropdown">
            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
              全部
              <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
              <li><a id="result-all" href="#" onclick="selected(id)">全部</a> </li>
              <li><a id="resultsse" href="#" onclick="selected(id)">sse</a></li>
              <li><a id="result-youth" href="#" onclick="selected(id)">校团委</a></li>
              <li><a id="result-news" href="#" onclick="selected(id)">新闻网</a></li>
            </ul>
          </div>
        </div>
        <div class="container" id="formDiv">
          <div>
              <form>
                  <input id="inputBox" type="text">
                  <input id="searchButton" type="submit" value="搜索" style="color: white">
              </form>
          </div>
        </div>
      </div>
      <script>
        document.getElementById("formDiv").style.width = window.screen.width.toString()+"px";
      </script>
      <br><br><br><br>
      <div id="search-result">
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="scripts/jquery.min.js"></script>
    <script src="scripts/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="scripts/ie10-viewport-bug-workaround.js"></script>

  <script>
    var field = 'all';
    function selected(id){
      var item = document.getElementById(id);
      var title = item.innerHTML.toString();
      field = title;
      document.getElementById("dropdownMenu1").innerHTML = title;
    }
  </script>


  <script>
      $(document).keydown(function (event) {
        if (event.which == 13) {
          console.log('start search');
          var searchKey = $('#search-input').val();
          $('body').css('background-image', 'null.png');
          if (searchKey !== '') {
            $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/s?key=' + $('#search-input').val() + '&field=' + field,
            success: function (data) {
              console.log(data);
              loadResultPage(data);
            }
          });
          } else {
            alert('请输入您想搜索的内容');
          }
        }
      })
      
      function loadResultPage(resultList) {
        console.log(resultList);
        $('#index').hide();
        $('#result').show();
        
        //在result中填入结果
         if (resultList.length > 0) {
           for (var i = 0; i < resultList.length; i++) {
             var resultElement = resultList[i];
             var newNode = document.createElement('div');
             newNode.className = 'resultItem';
             var newNodeTitle = document.createElement('h3');
             var newNodeContent = document.createElement('p');
             var newNodeUrl = document.createElement('a');
             var newHr = document.createElement('hr');
             newNodeTitle.innerHTML = resultElement.title;
             newNodeContent.innerHTML = resultElement.content;
             newNodeUrl.innerHTML = resultElement.url;
             newNodeUrl.setAttribute('href', resultElement.url);
             newNodeUrl.setAttribute('target', '_blank');
            
             newNode.appendChild(newNodeTitle);
             newNode.appendChild(newNodeContent);
             newNode.appendChild(newNodeUrl);
             newNode.appendChild(newHr);
             document.getElementById('search-result').appendChild(newNode);
           }
         } else {
           //显示搜索结果为0
           var nullTitle = document.createElement('h2');
           nullTitle.innerHTML = '抱歉，未搜索到结果';
           document.getElementById('search-result').appendChild(nullTitle);
         }
      }
    </script>
  </body>
</html>
